//
//  AuthorizationUtil.m
//  CheTuoTuo
//
//  Created by Houge on 2021/1/23.
//

#import "AuthorizationUtil.h"
#import "NSObject+BWAlert.h"

@implementation AuthorizationUtil

/// 通讯录权限
+ (void)checkAddressBookAuthorization:(AuthorizationSuccessBlock)successBlock {
    CNContactStore *addressBook = [[CNContactStore alloc] init];
    CNAuthorizationStatus authStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];;
    
    if (authStatus == CNAuthorizationStatusDenied) {
        [self showAlertViewWithTitle:@"无法访问通讯录" message:@"请在iPhone的""设置-隐私-通信录""中允许访问通讯录" confirmTitle:@"确定"];
        
    } else if (authStatus == CNAuthorizationStatusNotDetermined) {
        [addressBook requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (error){
                    if (error.code == 100) {
                        [self showAlertViewWithTitle:@"无法访问通讯录" message:@"请在iPhone的""设置-隐私-通信录""中允许访问通讯录" confirmTitle:@"确定"];
                    }
                    
                } else if (!granted) {
                    [self showAlertViewWithTitle:@"无法访问通讯录" message:@"请在iPhone的""设置-隐私-通信录""中允许访问通讯录" confirmTitle:@"确定"];
                    
                } else {
                    successBlock();
                }
            });
        }];
    }
    else {
        successBlock();
    }
}

/// 定位权限
+ (void)checkUserLocationAuthorization:(AuthorizationSuccessBlock)successBlock {
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if ([CLLocationManager locationServicesEnabled] && status != kCLAuthorizationStatusDenied) {
        //定位功能可用
        successBlock();
    }
    else {
        //定位不能用
        [self showAlertViewWithTitle:@"请先开启定位服务" message:@"请在iPhone的""设置-隐私-定位服务""中允许访问位置信息" confirmTitle:@"确定"];
    }
}

/// 相机权限
+ (void)checkCamareAuthorization:(AuthorizationSuccessBlock)successBlock {
    // 拍照前先检查相关权限
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
        // 无相机权限
        [self showAlertViewWithTitle:@"无法使用相机" message:@"请在iPhone的""设置-隐私-相机""中允许访问相机" confirmTitle:@"确定"];
    }
    else if (authStatus == AVAuthorizationStatusNotDetermined) {
        // fix issue 466, 防止用户首次拍照拒绝授权时相机页黑屏
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (granted) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    successBlock();
                });
            } else {
                [self showAlertViewWithTitle:@"无法使用相机" message:@"请在iPhone的""设置-隐私-相机""中允许访问相机" confirmTitle:@"确定"];
            }
        }];
    } else {
        successBlock();
    }
}

/// 相册权限
+ (void)checkPhotoLibraryAuthorization:(AuthorizationSuccessBlock)successBlock {
    
    // 拍照之前还需要检查相册权限
    if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusDenied) {
        // 已被拒绝，没有相册权限，将无法保存拍的照片
        [self showAlertViewWithTitle:@"无法访问相册" message:@"请在iPhone的""设置-隐私-相册""中允许访问相册" confirmTitle:@"确定"];
    }
    else if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusNotDetermined) { // 未请求过相册权限
        // 获取权限
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    successBlock();
                });
            } else {
                [self showAlertViewWithTitle:@"无法访问相册" message:@"请在iPhone的""设置-隐私-相册""中允许访问相册" confirmTitle:@"确定"];
            }
        }];
    } else {
        successBlock();
    }
}

/// 麦克风权限
+ (void)checkMicrophoneAuthorization:(AuthorizationSuccessBlock)successBlock {
    AVAudioSessionRecordPermission permissionStatus = [[AVAudioSession sharedInstance] recordPermission];
    
    if (permissionStatus == AVAudioSessionRecordPermissionDenied) {
        [self showAlertViewWithTitle:@"无法使用麦克风" message:@"请在iPhone的""设置-隐私-麦克风""中允许访问麦克风" confirmTitle:@"确定"];
    }
    else if (permissionStatus == AVAudioSessionRecordPermissionUndetermined) {
        [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
            if (granted) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    successBlock();
                });
            } else {
                [self showAlertViewWithTitle:@"无法使用麦克风" message:@"请在iPhone的""设置-隐私-麦克风""中允许访问麦克风" confirmTitle:@"确定"];
            }
        }];
    }
    else {
        successBlock();
    }
}

@end
